<template>
  <div>
    <el-dialog
      title="手术"
      :visible.sync="addDiavisible"
      width="50%"
      @close="cancel"
      @opened="beforeopen"
    >
      <el-form
        ref="formRef"
        :rules="formRules"
        :model="form"
        label-width="110px"
      >
        <el-form-item label="手术名称" prop="operationName">
          <el-input v-model="form.operationName"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="operationTime">
          <el-date-picker
            v-model="form.operationTime"
            type="date"
            placeholder="选择日期"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { inputValidator } from "@/utils/validate";
export default {
  data() {
    return {
      addDiavisible: false,
      form: {
        operationName: "",
        operationTime: "",
      },

      formRules: {
        operationName: inputValidator,
        operationTime: inputValidator,
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      for (let key in this.form) {
        this.form[key] = "";
      }
      this.addDiavisible = false;
    },
    beforeopen() {
      this.$refs.formRef.clearValidate();
    },
    //提交
    addSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return this.$message.error("请输入必填项!");
        var date = new Date(this.form.operationTime);
        this.form.operationTime =
          date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate();
        this.$emit("transData", this.form);
        this.cancel();
      });
    },
    //供父组件调用函数
    openDialog() {
      this.addDiavisible = true;
    },
  },
};
</script>